package com.blueocean_health.care.management.util;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * 更新通用角色的权限
 *
 *
 * @Title: UpdateRolePowerUtils.java
 * @Package com.blueocean_health.care.management.util
 * @author luxue
 * @date 2018年8月15日
 * @version V1.0
 */
public class UpdateRolePowerUtils {
	// 项目负责人角色id：10,11,12
	static int[] ids_1 = { 10, 11, 12 };
	// 省总(所有管理层)角色id：13,14,15
	static int[] ids_2 = { 13, 14, 15 };
	// 项目(副)经理角色id：16,17,18
	static int[] ids_3 = { 16, 17, 18 };
	// 外勤角色id：19,20,21
	static int[] ids_4 = { 19, 20, 21 };
	// 内勤角色id：22,23,24
	static int[] ids_5 = { 22, 23, 24 };

	/**
	 * 需要添加的权限
	 */
	// 项目负责人
	static String[] add_powerCodes_1 = new String[] { "1-4-10", "201-8-1" };

	// 省总(所有管理层)
	static String[] add_powerCodes_2 = new String[] { "201-8-1" };

	// 项目(副)经理
	static String[] add_powerCodes_3 = new String[] { "1-4-10" };

	// 外勤
	static String[] add_powerCodes_4 = new String[] { "1-4-10" };

	// 内勤
	static String[] add_powerCodes_5 = new String[] { "1-4-10" };

	/**
	 * 需要删除的权限
	 */
	static String[] del_powerCodes = new String[] { "1-4-10", "201-8-1" };

	public static void main(String[] args) throws SQLException {
		Connection conn = getConn();
		updateRole(conn);
		conn.close();
	}

	/**
	 * 更新role,role_power表的相关数据
	 */
	private static void updateRole(Connection conn) {
		// 删除的权限
		deletePower(conn);

		// 添加权限
		insertPower(ids_1, add_powerCodes_1, conn);
		System.out.println("-------------------------------");
		insertPower(ids_2, add_powerCodes_2, conn);
		System.out.println("-------------------------------");
		insertPower(ids_3, add_powerCodes_3, conn);
		System.out.println("-------------------------------");
		insertPower(ids_4, add_powerCodes_4, conn);
		System.out.println("-------------------------------");
		insertPower(ids_5, add_powerCodes_5, conn);

		System.out.println("-------------------------------");
		System.out.println("-------------------------------");
		System.out.println("-------------------------------");
		System.out.println("-------------------------------");
		System.out.println("-------------------------------");
	}

	/**
	 * 删除权限
	 * 
	 */
	private static void deletePower(Connection conn) {
		for (int j = 0; j < del_powerCodes.length; j++) {
			String powerCode = del_powerCodes[j];
			String sql = String.format("delete from role_power where power_code='%s'", powerCode);
			int result = delete(conn, sql);
			if (result == 1) {
				System.out.println("删除权限成功：" + sql);
			} else {
				System.err.println("删除权限失败：" + sql);
			}
		}
	}

	/**
	 * 添加权限
	 */
	private static void insertPower(int[] ids, String[] add_powerCodes, Connection conn) {
		for (int i = 0; i < ids.length; i++) {
			int id = ids[i];
			for (int j = 0; j < add_powerCodes.length; j++) {
				String powerCode = add_powerCodes[j];
				String sql = String.format(
						"INSERT INTO `role_power` (`id`, `power_code`, `role_id`, `is_active`, `create_time`) VALUES (null, '%s', '%s', '1', now());",
						powerCode, id);
				int result = insert(conn, sql);
				if (result == 1) {
					System.out.println("添加权限成功：" + sql);
				} else {
					System.err.println("添加权限失败：" + sql);
				}
			}
		}
	}

	/**
	 * 返回Connection，需要手动关闭Connection
	 */
	private static Connection getConn() {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://192.168.1.88:3306/txkh_nurse_platform";
		String username = "lhjk";
		String password = "lhjk123!";
		Connection conn = null;
		try {
			Class.forName(driver); // classLoader,加载对应驱动
			conn = (Connection) DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	private static int insert(Connection conn, String sql) {
		int i = 0;
		PreparedStatement pstmt;
		try {
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			i = pstmt.executeUpdate();
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}

	private static int delete(Connection conn, String sql) {
		int i = 0;
		PreparedStatement pstmt;
		try {
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			i = pstmt.executeUpdate();
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
}
